<!--
 * @Descripttion: 
 * @Author: MajorMayer
 * @Date: 2021-08-09 18:17:36
 * @LastEditors: 周杨
 * @LastEditTime: 2021-09-23 15:13:10
-->
<template>
    <div v-loading="loading" ref="subappViewport"></div>
</template>

<script>
import to from 'await-to-js'
import { loadMicroApp } from "qiankun"
import { GetLatestEnrollmentBatch, WhetherFillInEnrollmentRecordApi } from "@/api/activity"

export default {
    props: {
        redirect: { type: String, default: "" }
    },
    data() {
        return { loading: true, microApp: null }
    },
    async mounted() {
        if (!this.$route.query.formid) {
            var [err, res] = await to(GetLatestEnrollmentBatch())
            if (err) return history.go(-1)
            var data = res.data.Data?.[0]
            if (!data?.FormId || !data?.F_ActivityId) return this.$message.error({ message: "参数错误" })

            var [err, res] = await to(WhetherFillInEnrollmentRecordApi({ "FormId": data.FormId, "UserId": this.$store.state.userId }))
            if (err) return alert('接口异常')
            var wetherFillInEnrollmentRecordApidata = res.data.Data

            if (wetherFillInEnrollmentRecordApidata) this.$router.push({ path: '/user/admission' });
            else this.qiankunStart(`?formid=${data.FormId}&F_ActivityId=${data.F_ActivityId}&redirect=${this.redirect}`)
        }
        else {
            const { formid, F_ActivityId } = this.$route.query || {}
            if (!formid || !F_ActivityId) return this.$message.error({ message: "参数错误" })
            this.qiankunStart(`?formid=${formid || "zhaoshengbaoming"}&F_ActivityId=${F_ActivityId}&redirect=${this.redirect}`)
        }
    },
    beforeDestroy() {
        this.microApp?.unmount()
    },
    methods: {
        qiankunStart(params) {
            this.microApp = loadMicroApp({
                name: "custom_form",
                entry: "/Website/customForm/form",
                container: this.$refs.subappViewport,
                props: { params }
            }, {
                fetch(url, ...args) {
                    return window.fetch(url, {
                        headers: { Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" },
                        ...args
                    })
                },
                singular: true
            })
        }
    }
};
</script>


 
